package com.hyyt.shopjsp.basic.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.redis.util.RedisDataReadUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hyyt.shopjsp.basic.pojo.BasicStatistics;
import com.hyyt.shopjsp.basic.service.IBasicStatisticsService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Wang Youchun on 2017/3/21 0024下午 5:42.
 */
@Controller
@RequestMapping("/back/statistics")
public class BasicStatisticsController extends BaseController {
	private Logger log = Logger.getLogger(BasicStatisticsController.class);
	private String basePath = "/back/basic/statistics/";

	@Resource
	private IBasicStatisticsService basicStatisticsService;

	/**
	 * 进入系统欢迎页管理
	 * @return 进入指定页面
	 */
	@RequestMapping("gotoStatisticsPage")
	public String gotoStatisticsPage(){
		return basePath+"statisticsList";
	}

	/**
	 * 查询所有统计表
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="listStatistics",method=RequestMethod.POST)
	@ResponseBody
	public void listStatistics(Page page, String isShow,String statisticsCode,String statisticsName,HttpServletResponse response){
		Map<String, Object> param = new HashMap<>();
		if (Utils.objectIsNotEmpty(statisticsCode)) {//名称
			param.put("statisticsCode", statisticsCode.trim());
		}
		if (Utils.objectIsNotEmpty(statisticsName)) {//类型名称
			param.put("statisticsName", statisticsName.trim());
		}
		if (Utils.objectIsNotEmpty(isShow)) {//显示状态
			param.put("isShow",isShow);
		}
		param.put("sortColumns", "statisticsId desc");
		int totalRecordCount =basicStatisticsService.countBasicStatistics(param);
		page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
		List<BasicStatistics> list =basicStatisticsService.queryBasicStatistics(param, page.getPageRecordBeginIndex(), page.getPageSize());
		param.clear();
		param.put("total", totalRecordCount);// total键 存放总记录数，必须的
		param.put("rows", list);// rows键 存放每页记录 list
		ResultMsg.create(param, response);  //把内容发送到页面
	}

	/**
     * 根据id获得对象信息
     *
     * @param statisticsId
     */
    @RequestMapping(value = "getStatisticsInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg getStatisticsInfo(Integer statisticsId) {
        if (Utils.objectIsNotEmpty(statisticsId)) {
            BasicStatistics basicStatistics = basicStatisticsService.loadBasicStatistics(statisticsId);
            return ResultMsg.create(true, null, FastJsonUtils.toJSONString(basicStatistics));
        }else {
			return ResultMsg.create(false);
		}
    }

    /**
     * 保存或者修改对象信息
     *
     * @param basicStatistics
     * @return
     */
    @RequestMapping(value = "saveOrUpdateStatistics", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateStatistics(BasicStatistics basicStatistics) {
        if (Utils.objectIsNotEmpty(basicStatistics)) {
            if (Utils.objectIsEmpty(basicStatistics.getStatisticsId())) {
            	basicStatisticsService.saveBasicStatistics(basicStatistics);;
            } else {
            	Timestamp date = new Timestamp(System.currentTimeMillis());//获取timestamp类型的当前系统时间
            	basicStatistics.setUpdateTime(date);
            	basicStatisticsService.updateBasicStatistics(basicStatistics);
            }
			return ResultMsg.create(true);
        }else {
			return ResultMsg.create(false);
		}
    }

    /**
     * 根据传入id删除对象信息
     *
     * @param ids
     */
    @RequestMapping(value = "deleteStatistics", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteStatistics(String ids) {
        if (Utils.objectIsNotEmpty(ids)) {
        	basicStatisticsService.deleteBasicStatistics(Integer.parseInt(ids));
			return ResultMsg.create(true);
        }else {
			return ResultMsg.create(false);
		}
    }

      /**
       * 同步统计表
       * @param response
       */
      @RequestMapping(value="initStatisticsHome",method=RequestMethod.POST)
      @ResponseBody
      public void initStatisticsHome(HttpServletResponse response){
    	  Map<String, Object> param = new HashMap<>();
          String statisticsJsonString = RedisDataReadUtil.getRedisValue(StringStaticKey.BACK_HOME_STATISTICS);
          if(Utils.objectIsEmpty(statisticsJsonString)){
              param.put("isShow", "1");
              List<BasicStatistics> statisticsList=basicStatisticsService.querySomeBasicStatistics(param);
              param.clear();
              statisticsJsonString = FastJsonUtils.toJSONString(statisticsList);//将value格式化成json格式
              redisBasicService.setKey(StringStaticKey.BACK_HOME_STATISTICS,statisticsJsonString);
          }
    	  param.put("statisticsList", statisticsJsonString);
          ResultMsg.create(param, response);  //把内容发送到页面
      }
}